package com.truonghau.smstoxls.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sun.xml.internal.bind.v2.model.core.Ref;
import com.truonghau.smstoxls.R;
import com.truonghau.smstoxls.receiver.IncomingSmsBroadcastReceiver;
import com.truonghau.xmeasure.commons.Constants;
import com.truonghau.xmeasure.commons.SmSUtils;

public class MyGPSActivity extends Activity {
	Button btnStopService;    
	TextView txtMsgNotif, txtTelNumber, txtMsgFormat, txtBankBa, txtMoneyBa;
	Intent  intentMyService;
	ComponentName service;
	BroadcastReceiver receiver;

	String SMS_FILTER = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_test_activity);
		txtMsgNotif = (TextView) findViewById(R.id.txtMsgNotif);
		txtTelNumber = (TextView) findViewById(R.id.txtTelNumber);
		txtMsgFormat = (TextView) findViewById(R.id.txtFormat);
		txtBankBa = (TextView) findViewById(R.id.txtBankB);
		txtMoneyBa = (TextView) findViewById(R.id.txtMoneyB);
		//load
		SharedPreferences settings = getSharedPreferences(
				Constants.PREFS_NAME, 0);

		String txtMsgNotifStr = settings.getString(Constants.TxtMsgNotif, "");
		String txtTelNumberStr = settings.getString(Constants.TxtTelNumber, "");
		String txtMsgFormatStr = settings.getString(Constants.TxtMsgFormat1, "");
		String txtBankBStr = settings.getString(Constants.TxtBankB, "");
		String txtMoneyBStr = settings.getString(Constants.TxtMoneyB, "");
		txtMsgNotif.setText(txtMsgNotifStr);
		txtTelNumber.setText(txtTelNumberStr);
		txtMsgFormat.setText(txtMsgFormatStr);
		txtBankBa.setText(txtBankBStr);
		txtMoneyBa.setText(txtMoneyBStr);
	}
	public void onClickStartBtn(View view){
		try {
			IntentFilter mainFilter = new IntentFilter(SMS_FILTER);
			receiver = new IncomingSmsBroadcastReceiver();
			registerReceiver(receiver, mainFilter);
			SmSUtils.startSMSAlert(this);
			Toast.makeText(this, "Đã bắt đầu chạy ngầm!", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Log.e("error", e.getMessage());
			Toast.makeText(this, "Lỗi, kiểm tra lại!", Toast.LENGTH_LONG).show();
		}
	}
	public void onClickSaveBtn(View view){
		try {
			SharedPreferences settings = getSharedPreferences(
					Constants.PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString(Constants.TxtMsgNotif,
					txtMsgNotif.getText().toString());
			editor.putString(Constants.TxtTelNumber,
					txtTelNumber.getText().toString());
			editor.putString(Constants.TxtMsgFormat1,
					txtMsgFormat.getText().toString());
			editor.putString(Constants.TxtBankB,
					txtBankBa.getText().toString());
			editor.putString(Constants.TxtMoneyB,
					txtMoneyBa.getText().toString());
			editor.commit();
			Toast.makeText(this, "Đã lưu các tham số!", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Log.e("error", e.toString());
			Toast.makeText(this, "Lỗi, kiểm tra lại!", Toast.LENGTH_LONG).show();
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	public void quangcao1(View view){
		getPopupAdv(view.getContext(), "Thêm ứng dụng", "Mời bạn ghé thăm các ứng dụng trong cửa hàng của công ty THSoft");
	}

	private  void  getPopupAdv(final  Context context, String title, String sms) {
		AlertDialog.Builder alert = new  AlertDialog.Builder(context);
		alert.setTitle(title);
		alert.setMessage(sms);
		alert.setIcon(R.drawable.iconlogo);
		alert.setPositiveButton(context.getString(android.R.string.ok), new  DialogInterface.OnClickListener() {
			public  void  onClick(DialogInterface dialog, int  whichButton) {
				String url = "market://search?q=thsoft co&c=apps";
				if(Constants.APPOTA_STORE){
					url ="http://appstore.vn/a/index.php/home/app/6308";
				}
				Intent intent = new Intent(Intent.ACTION_VIEW, 
						Uri.parse(url));
				context.startActivity(intent);
			}
		});
		alert.setNegativeButton(context.getString(android.R.string.cancel), new  DialogInterface.OnClickListener() {
			public  void  onClick(DialogInterface dialog, int  whichButton) {

			}
		});
		alert.show();
	}
	public void huongdan1(View view){
		Intent myIntent = new Intent(view.getContext(), huongdan.class);
		startActivity(myIntent);}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setTitle("Thoát ứng dụng");
			adb.setIcon(R.drawable.exitmenu);
			adb.setMessage("Bạn có thực sự muốn thoát ứng dụng?");
			adb.setCancelable(false);
			adb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					MyGPSActivity.this.finish();

				}
			});
			adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();

				}
			});

			adb.show();

			break;
		}
		return super.onOptionsItemSelected(item);
	}





}


